package td

import (
	"context"
	"encoding/json"
	. "ez/apps/erp/document"
	"ez/apps/td/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Td struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64       `json:"id" bson:"id,omitempty"`
	Title       string      `json:"title" bson:"title"`             //标题
	Content     string      `json:"content" bson:"content"`         //正文
	Cat         *TdCategory `json:"cat" bson:"cat"`                 //待办分类
	CatId       int64       `json:"catId" bson:"catId"`             //待办分类
	TargetEmp   *Emp        `json:"targetEmp" bson:"targetEmp"`     //责任人
	TargetEmpId int64       `json:"targetEmpId" bson:"targetEmpId"` //责任人
	CreateEmp   *Emp        `json:"createEmp" bson:"createEmp"`     //创建人
	CreateEmpId int64       `json:"createEmpId" bson:"createEmpId"` //创建人
	AuditEmp    *Emp        `json:"auditEmp" bson:"auditEmp"`       //审核人
	AuditEmpId  int64       `json:"auditEmpId" bson:"auditEmpId"`   //审核人
	AuditAt     time.Time   `json:"auditAt" bson:"auditAt"`         //审核时间
	PublishAt   time.Time   `json:"publishAt" bson:"publishAt"`     //发布时间
	Reads       int64       `json:"reads" bson:"reads"`             //阅读量
	ReadEmps    []*Emp      `json:"readEmps" bson:"readEmps"`       //阅读用户
	ReadEmpsIds []int64     `json:"readEmpsIds" bson:"readEmpsIds"` //阅读用户
	CreateAt    time.Time   `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time   `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time  `json:"deleteAt" bson:"deleteAt"`
}

func (this *Td) DocName() string { return "Td" }
func (this *Td) GetId() int64    { return this.Id }
func (this *Td) SetId(id int64)  { this.Id = id }
func (this *Td) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Td) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Td) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Td) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Td) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Td) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Td) ToString() string {
	return string(this.ToBytes())
}
func (this *Td) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Td) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Td) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Td) LoadCat() {
	if this.CatId == 0 {
		return
	}
	this.Cat, _ = NewTdCategoryCrud().FindId(this.CatId)
}
func (this *Td) LoadTargetEmp() {
	if this.TargetEmpId == 0 {
		return
	}
	this.TargetEmp, _ = NewEmpCrud().FindId(this.TargetEmpId)
}
func (this *Td) LoadCreateEmp() {
	if this.CreateEmpId == 0 {
		return
	}
	this.CreateEmp, _ = NewEmpCrud().FindId(this.CreateEmpId)
}
func (this *Td) LoadAuditEmp() {
	if this.AuditEmpId == 0 {
		return
	}
	this.AuditEmp, _ = NewEmpCrud().FindId(this.AuditEmpId)
}
func (this *Td) LoadReadEmps() {
	if this.ReadEmps == nil {
		this.ReadEmps = make([]*Emp, 0)
	}
	if this.ReadEmpsIds == nil {
		this.ReadEmpsIds = make([]int64, 0)
	}
	if len(this.ReadEmpsIds) > 0 {
		this.ReadEmps = make([]*Emp, 0)
		for _, v := range this.ReadEmpsIds {
			find, e := NewEmpCrud().FindId(v)
			if e == nil {
				this.ReadEmps = append(this.ReadEmps, find)
			}
		}
	}
}
func (this *Td) ClearRelationsBeforeSave() mgo.Doc {
	this.Cat = nil
	this.TargetEmp = nil
	this.CreateEmp = nil
	this.AuditEmp = nil
	this.ReadEmps = nil
	return this
}
func neverUsed_Td() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type TdAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Td
	Session *ez.Session
}

func NewTdAccessControl(model *Td, action string, session *ez.Session) *TdAccessControl {
	ctrl := &TdAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.TdAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
